home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / gnucdiff / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-16  |  6.7 KB  |  264 lines

  1. /* Read, sort and compare two directories.  Used for GNU DIFF.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21. #include "diff.h"
  22.  
  23. static int compare_names ();
  24.  
  25. /* Read the directory named DIRNAME and return a sorted vector
  26.    of filenames for its contents.  */
  27.  
  28. struct dirdata
  29. {
  30.   int length;            /* # elements in `files' */
  31.   char **files;            /* Sorted names of files in the dir */
  32. };
  33.  
  34. #if defined(MSDOS)
  35. /*
  36. ** due to difference of opinion btw gnu and microsoft about what
  37. ** const means, const is defined away in diff.h, which causes warnings
  38. ** when compiling the headers.  This ugliness is avoided here.
  39. */
  40. #ifdef const
  41. #undef const
  42. #endif
  43. #include <string.h>
  44. #include <dos.h>
  45.  
  46. struct direct {
  47.     char d_name[14];
  48. };
  49.  
  50. typedef struct _dir {
  51.     int first;
  52.     struct find_t dta;
  53.     struct direct current;
  54. } DIR;
  55.  
  56.  
  57. DIR *
  58. opendir(char *name) {
  59.     char localname[65];
  60.     DIR *rval = malloc(sizeof(DIR));
  61.     strcpy(localname,name);
  62.     strcat(localname,"\\*.*");
  63.     if(rval == NULL ||
  64.         _dos_findfirst(localname,_A_NORMAL|_A_SUBDIR,&rval->dta) != 0)
  65.         return NULL;
  66.     rval->first = 1;
  67.     return rval;
  68. }
  69.  
  70. void
  71. closedir(DIR *x) {
  72.     free(x);
  73. }
  74.  
  75. struct direct *
  76. readdir(DIR *thisdir) {
  77.     /*
  78.     ** first time through, we don't need to look for a file
  79.     */
  80.     if(!thisdir->first) {
  81.         if(_dos_findnext(&thisdir->dta) != 0)
  82.             return NULL;
  83.     } else
  84.         thisdir->first = 0;
  85.     strncpy(thisdir->current.d_name,thisdir->dta.name,13);
  86.     thisdir->current.d_name[13] = '\0';
  87.     strlwr(thisdir->current.d_name);
  88.     return &thisdir->current;
  89. }
  90.         
  91. #endif /* MSDOS */
  92.  
  93. static struct dirdata
  94. dir_sort (dirname)
  95.      char *dirname;
  96. {
  97.   register DIR *reading;
  98.   register struct direct *next;
  99.   struct dirdata dirdata;
  100.   int compare_names ();
  101.  
  102.   /* Address of block containing the files that are described.  */
  103.   char **files;
  104.  
  105.   /* Length of block that `files' points to, measured in files.  */
  106.   int nfiles;
  107.  
  108.   /* Index of first unused in `files'.  */
  109.   int files_index;
  110.  
  111.   /* Open the directory and check for errors.  */
  112.   reading = opendir (dirname);
  113.   if (!reading)
  114.     {
  115.       pfatal_with_name (dirname);
  116.       return dirdata;
  117.     }
  118.  
  119.   /* Initialize the table of filenames.  */
  120.  
  121.   nfiles = 100;
  122.   files = (char **) xmalloc (nfiles * sizeof (char *));
  123.   files_index = 0;
  124.  
  125.   /* Read the directory entries, and insert the subfiles
  126.      into the `files' table.  */
  127.  
  128.   while (next = readdir (reading))
  129.     {
  130.       /* Ignore the files `.' and `..' */
  131.       if (next->d_name[0] == '.'
  132.       && (next->d_name[1] == 0
  133.           || (next->d_name[1] == '.'
  134.           && next->d_name[2] == 0)))
  135.     continue;
  136.  
  137.       if (files_index == nfiles)
  138.     {
  139.       nfiles *= 2;
  140.       files
  141.         = (char **) xrealloc (files, sizeof (char *) * nfiles);
  142.     }
  143.       files[files_index++] = concat (next->d_name, "", "");
  144.     }
  145.  
  146.   closedir (reading);
  147.  
  148.   /* Sort the table.  */
  149.   qsort (files, files_index, sizeof (char *), compare_names);
  150.  
  151.   /* Return a description of location and length of the table.  */
  152.   dirdata.files = files;
  153.   dirdata.length = files_index;
  154.  
  155.   return dirdata;
  156. }
  157.  
  158. /* Sort the files now in the table.  */
  159.  
  160. static int
  161. compare_names (file1, file2)
  162.      char **file1, **file2;
  163. {
  164.   return strcmp (*file1, *file2);
  165. }
  166.  
  167. /* Compare the contents of two directories named NAME1 and NAME2.
  168.    This is a top-level routine; it does everything necessary for diff
  169.    on two directories.
  170.  
  171.    HANDLE_FILE is a caller-provided subroutine called to handle each file.
  172.    It gets five operands: dir and name (rel to original working dir) of file
  173.    in dir 1, dir and name pathname of file in dir 2, and the recursion depth.
  174.  
  175.    For a file that appears in only one of the dirs, one of the name-args
  176.    to HANDLE_FILE is zero.
  177.  
  178.    DEPTH is the current depth in recursion.
  179.  
  180.    Returns the maximum of all the values returned by HANDLE_FILE.  */
  181.  
  182. int
  183. diff_dirs (name1, name2, handle_file, depth)
  184.      char *name1, *name2;
  185. #if !defined(MSDOS)
  186.      int (*handle_file) ();
  187. #else
  188.     /* sorry, rms, I can't live with the assumption that
  189.     ** sizeof(char *) == sizeof(int)
  190.     */
  191.     int (*handle_file)(char *dir0,char *name0,
  192.         char *dir1,char *name1,int depth);
  193. #endif
  194. {
  195.   struct dirdata data1, data2;
  196.   register int i1, i2;
  197.   int val = 0;
  198.   int v1;
  199.  
  200.   /* Get sorted contents of both dirs.  */
  201.   data1 = dir_sort (name1);
  202.   data2 = dir_sort (name2);
  203.  
  204.   i1 = 0;
  205.   i2 = 0;
  206.  
  207.   /* If -Sname was specified, and this is the topmost level of comparison,
  208.      ignore all file names less than the specified starting name.  */
  209.  
  210.   if (dir_start_file && depth == 0)
  211.     {
  212.       while (i1 < data1.length && strcmp (data1.files[i1], dir_start_file) < 0)
  213.     i1++;
  214.       while (i2 < data2.length && strcmp (data2.files[i2], dir_start_file) < 0)
  215.     i2++;
  216.     }
  217.  
  218.   /* Loop while files remain in one or both dirs.  */
  219.   while (i1 < data1.length || i2 < data2.length)
  220.     {
  221.       int nameorder;
  222.  
  223.       /* Compare next name in dir 1 with next name in dir 2.
  224.      At the end of a dir,
  225.      pretend the "next name" in that dir is very large.  */
  226.  
  227.       if (i1 == data1.length)
  228.     nameorder = 1;
  229.       else if (i2 == data2.length)
  230.     nameorder = -1;
  231.       else
  232.     nameorder = strcmp (data1.files[i1], data2.files[i2]);
  233.  
  234.       if (nameorder == 0)
  235.     {
  236.       /* We have found a file (or subdir) in common between both dirs.
  237.          Compare the two files.  */
  238.       v1 = handle_file (name1, data1.files[i1], name2, data2.files[i2],
  239.                 depth + 1);
  240.       i1++, i2++;
  241.     }
  242.       if (nameorder < 0)
  243.     {
  244.       /* Next filename in dir 1 is less; that is a file in dir 1 only.  */
  245.       v1 = handle_file (name1, data1.files[i1], name2, (char *)0,
  246.           depth + 1);
  247.       i1++;
  248.     }
  249.       if (nameorder > 0)
  250.     {
  251.       /* Next filename in dir 2 is less; that is a file in dir 2 only.  */
  252.       v1 = handle_file (name1, (char *)0, name2, data2.files[i2],
  253.           depth + 1);
  254.       i2++;
  255.     }
  256.       if (v1 > val)
  257.     val = v1;
  258.     }
  259.   free (data1.files);
  260.   free (data2.files);
  261.  
  262.   return val;
  263. }
  264.